<?php
class GalInfo{
    var $name;
    var $photos;
    function GalInfo(){
        $photos=array();
    }

}
$link = mysql_connect('localhost', 'root', '1');
if (!$link) {
    die('<br>Ошибка соединения: ' . mysql_error());
}

mysql_query ('SET NAMES utf8');
mysql_query ('SET CHARACTER SET utf8');
mysql_set_charset('utf8',$link);

$db_selected = mysql_select_db('Melagrano', $link);

$select=" select ";
$mainSql = "FROM melagranoru.titems_internet a
inner join melagranoru.tmatchingphotos b
on a.Article = b.linkArticle
inner join melagranoru.tconstme c
on a.Me = c.id
inner join melagranoru.tconstnames d
on d.id = a.Name";

$sql = "a.Article, a.`Vstavka_text`, a.`Origin country`, a.Qty,
a.Weight, a.Size, a.Colour, a.Price, b.FileName,b.Order,
c.`Металл`, d.Name
 ".$mainSql;

if ( $_SERVER['REQUEST_METHOD'] == "GET" ) {
    $extra = array_key_exists('Article', $_GET) ? " WHERE Article = '".$_GET['Article']."'" : null;
    $order=" order by b.Order";
    if($extra){
        $sql=$select.$sql.$extra.$order;
        $r = mysql_query($sql,$link);

        $items=new GalInfo();
        for($i = mysql_num_rows($r); $i > 0 ; $i--) {
            $content = mysql_fetch_assoc($r);
            if($i==1){
                $items->name=$content['Name'];
            }
            $items->photos[]=$content['FileName'];
        }

    echo json_encode($items);
    }
}
/** UPDATE or DELETE **/
else if ( array_key_exists ("_method", $_POST) ) {
    // Don't bother doing anything here. We don't really need it for the demo
}
/** CREATE **/
else {
    // Just return a model. Don't bother actually creating it and returning the new record
    $data = array( "id" => 1, "name" => "Joe Zim", "age" => 23 );
    // Encode it as JSON and ship it back
    echo json_encode($data);
}




?>
